package com.ruoyi.system.service.impl;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.config.SensorConfig;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.entity.LightningData;
import com.ruoyi.system.entity.LightningSensor;
import com.ruoyi.system.entity.SysMonitor;
import com.ruoyi.system.mapper.LightningDataMapper;
import com.ruoyi.system.mapper.LightningSensorMapper;
import com.ruoyi.system.service.LightningDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (LightningData)表服务实现类
 * @since 2023-03-08 13:46:31
 */
@Service("lightningDataService")
public class LightningDataServiceImpl implements LightningDataService {
    @Resource
    private LightningDataMapper lightningDataMapper;
    @Autowired
    private SensorConfig sensorConfig;

    /**
     * 通过ID查询单条数据
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public LightningData queryById(Integer id) {
        return this.lightningDataMapper.queryById(id);
    }

    /**
     * 分页查询
     * @param lightningData 筛选条件
     * @return 查询结果
     */
    @Override
    public List<LightningData> queryByPage(LightningData lightningData) {
        List<LightningData> lightningDataList = this.lightningDataMapper.queryAllByLimit(lightningData);
        return lightningDataList;
    }

    /**
     * 新增数据
     * @param lightningData 实例对象
     * @return 实例对象
     */
    @Override
    public LightningData insert(LightningData lightningData) {
        lightningData.setCreateTime(DateUtils.getNowDate());
        lightningData.setIsUpload(lightningData.getIsUpload()==null ? 0 : lightningData.getIsUpload());
        this.lightningDataMapper.insert(lightningData);
        return lightningData;
    }

    /**
     * 修改数据
     * @param lightningData 实例对象
     * @return 实例对象
     */
    @Override
    public LightningData update(LightningData lightningData) {
        this.lightningDataMapper.update(lightningData);
        return this.queryById(lightningData.getId());
    }

    /**
     * 通过主键删除数据
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.lightningDataMapper.deleteById(id) > 0;
    }

    // 同步雷电站点实时数据
//    @Scheduled(cron = "0/60 * * * * ?")
    public void synchSensor() {
        System.out.println("开始同步雷电站点实时数据");
        HttpResponse execute = HttpRequest.post("http://" + sensorConfig.getIp() + ":" + sensorConfig.getPort() + sensorConfig.getSynchData())
                .form("username", sensorConfig.getUserName(), "password", SensorConfig.getPASSWORD()).timeout(3000).execute();
        int status = execute.getStatus();
        if (status != 200) {
            return;
        }
        String body = execute.body();
        JSONArray jsonArray = JSONUtil.parseArray(body);
        Object o1 = jsonArray.get(0);
        LightningData lightningData = JSONUtil.toBean((JSONObject) o1, LightningData.class);
        this.insert(lightningData);
    }


    @Override
    public List<LightningData> queryYearOrMonthList(String startTime, String endTime) {
        // 雷电预警统计
       /* Map<String, Object> dangerMap = new HashMap<>();
        List<Map<String, Integer>> danger = lightningDataMapper.getLdyjCount(startTime,endTime);
        dangerMap.put("warning",danger);*/
        List<LightningData> dataList = lightningDataMapper.queryYearOrMonthList(startTime,endTime);
        return dataList;
    }
}
